#!/usr/bin/env python
# -*- coding:utf-8 -*-
import ssl

ssl._create_default_https_context = ssl._create_unverified_context  # 防止报错
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.sms.v20190711 import sms_client, models
from s25 import settings


def send_sms_single(phone_num, Template_id,  template_param_list):
    """
    单条发送短信
    示例：'+8617853680108', template_id, [str(code), ]
    :param phone_num: 手机号
    :param template_id: 腾讯云短信模板ID
    :param template_param_list: 短信模板所需参数列表，例如:【验证码：{1}，描述：{2}】，则传递参数 [888,666]按顺序去格式化模板
    :return:
    """
    Secret_id = settings.SecretId  # 自己应用ID
    Secret_key = settings.SecretKey  # 自己应用Key
    Sign = settings.Sign  # 自己腾讯云创建签名时填写的签名内容（使用公众号的话这个值一般是公众号全称或简称）
    SmsSdk_app_id = settings.SmsSdk_app_id

    try:
        cred = credential.Credential(Secret_id, Secret_key)
        httpProfile = HttpProfile()
        httpProfile.endpoint = "sms.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = sms_client.SmsClient(cred, "", clientProfile)

        req = models.SendSmsRequest()
        params = {
            "PhoneNumberSet": [phone_num],
            "TemplateParamSet": template_param_list,
            "TemplateID": Template_id,
            "SmsSdkAppid": SmsSdk_app_id,
            "Sign": Sign
        }
        req.from_json_string(json.dumps(params))

        resp = client.SendSms(req)

    except TencentCloudSDKException as err:
        print(err)
    return resp.to_json_string()
